import { headersStore } from '@/stores/apiHeader'
import { useHomeStore } from '@/stores/module/home'
import { useUserStore } from '@/stores/user'
import { roleStore } from '@/stores/role'
import { baseToolsStore } from '@/stores/baseTools'
import { tipMessage } from './Basic'
import { t } from '@/i18n'

const homeStore = useHomeStore()
const headStore = headersStore()
const theRoleStore = roleStore()
const { userInfo } = useUserStore() as any

export const mouseClick = (intersects, threeInfo, callBack) => {
  const { currentPath, quCode } = threeInfo
  for (let i = 0; i < intersects.length; i++) {
    if (
      intersects[i].object.name !== '地球' &&
      intersects[i].object.name !== 'mark' &&
      intersects[i].object.name !== '飞行线' &&
      intersects[i].object.name !== ''
    ) {
      // console.log(
      //   '🚀 ~当前点位 code:',
      //   intersects[i].object,
      //   intersects[i].object.name
      //   // intersects[i].object['scopeCode'],
      //   // intersects[i].object['scopeLevel']
      // )
      // 弹窗面板赋值
      // data.countryName = intersects[i].object.name
      let objList = {
        name: intersects[i].object.name,
        scopeCode: intersects[i].object['scopeCode'],
        isClick: intersects[i].object['isClick'],
      }
      // 只允许 isClick 为 true 并且包含 scopeCode 的对象点击执行移动相机
      const scopeCode = intersects[i].object['scopeCode']
      const scopeLevel = intersects[i].object['scopeLevel']
      const point_code = headStore.point_code
      const chinaRole = userInfo.bussinessCode.includes('0000111104')
      const tipMsg = t('bi_common_youdonthavepermisson')
      if (objList.isClick && scopeCode) {
        callBack(objList)
        baseToolsStore().setIsDepartment(false) // 清空部门状态
        headStore.setScopeCode(scopeCode)
        // theRoleStore.setRole(scopeLevel, scopeLevel, scopeLevel)
        if (currentPath == '/home') {
          theRoleStore.setBusinessLevel(
            !chinaRole && scopeCode === '0000111104' ? 3 : scopeLevel
          )
          setTimeout(() => {
            homeStore.getAll()
          }, 300)
        }
        if (currentPath == '/finance') {
          theRoleStore.setFinanceLevel(scopeLevel)
        }
        if (currentPath == '/supply') {
          theRoleStore.setSupplyLevel(scopeLevel)
        }
        if (currentPath == '/human') {
          theRoleStore.setHumanLevel(scopeLevel)
        }
        headStore.setEveryCode('') // 还原 everyCode
      } else if (!objList.isClick && objList.scopeCode === '0000111104') {
        console.log('中国区域')
        // 如果是中国区且有权限的情况下，就直接跳转到中国区的页面
        let hasChinaCode = () => {
          let n = point_code.length
          let code = 'false'
          for (let i = 0; i < n; i++) {
            if (point_code[i].indexOf('CN') != -1) {
              code = point_code[i]
              return code
            }
          }
          return code
        }
        if (hasChinaCode() == 'false') {
          tipMessage(tipMsg)
        } else if (!objList.isClick && hasChinaCode() != 'false') {
          // 数组是否包含部分字符串
          headStore.setScopeCode(hasChinaCode())
          headStore.setEveryCode('') // 还原 everyCode
          theRoleStore.setBusinessLevel(!chinaRole ? 3 : 2) // 如果是非中国区域全权限的情况下降级到 3 级
          callBack(objList)
        } else {
          headStore.setScopeCode(scopeCode)
          headStore.setEveryCode('') // 还原 everyCode
          callBack(objList)
        }
      } else {
        if (currentPath !== '/human' || quCode.includes(scopeCode)) {
          tipMessage(tipMsg)
        }
        return false
      }
      return false
    } else {
      // frameDivClose()
    }
  }
}

// 检索指定的图层
// const retrievalLayer = (name) => {
//   let layerObj = scene.getObjectByName(name)
//   infoDiv(layerObj.position.x, layerObj.position.y, layerObj.position.z)
// }

// 单独数据展示标签
// const infoDiv = (pointx, pointy, pointz) => {
//   // 坐标转换
//   let world_vector = new THREE.Vector3(pointx, pointy, pointz)
//   let vector = world_vector.project(camera)
//   let halfWidth = data.mapDom.offsetWidth / 2,
//     halfHeight = data.mapDom.offsetHeight / 2
//   let x = Math.round(vector.x * halfWidth + halfWidth)
//   let y = Math.round(-vector.y * halfHeight + halfHeight)
//   //创建div容器
//   let moonDiv = document.getElementById('layerMain')
//   moonDiv.style.display = 'block'
//   moonDiv.style.left = x - 150 + 'px'
//   moonDiv.style.top = y - 180 + 'px'
// }

// 关闭标签
// const frameDivClose = () => {
//   let divHtml = document.getElementById('layerMain')
//   divHtml.style.display = 'none'
// }
